package android.theHead;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.theHead.patDebug.HeadLocationReceiver;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class CurrentReadings extends Activity{
	
	public HeadReadings headLocation = null;
	public HeadLocationReceiver receiver = null;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.current_readings);
	}
	
	public void onResume(){
		super.onResume();
		Log.i("patDebug","onResume");
		this.startService(new Intent(this, BackgroundService.class));
		
		IntentFilter filter;
		filter = new IntentFilter(BackgroundService.NEW_HEAD_READINGS);
		receiver = new HeadLocationReceiver();
		registerReceiver(receiver, filter);
	}

	@Override
	protected void onPause() {
		super.onPause();
		this.stopService(new Intent(this, BackgroundService.class));
		unregisterReceiver(receiver); super.onPause();
		Log.i("patDebug", "onPause");
	}

	public void updateFields(){
		((TextView) findViewById(R.id.speedField)).setText(Float.toString(headLocation.speed));
		((TextView) findViewById(R.id.positionField)).setText(headLocation.latitude + " : " + headLocation.longitude);
	}
	
	public class HeadLocationReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			headLocation = intent.getParcelableExtra(BackgroundService.HEAD_READINGS_FIELD);
			updateFields();
			
		}
	}
}
